package cn.tedu.shoppingmall.controller;

import cn.tedu.shoppingmall.mapper.CommodityMapper;
import cn.tedu.shoppingmall.mapper.ShopMapper;
import cn.tedu.shoppingmall.vo.Commodity;
import cn.tedu.shoppingmall.vo.Shopcar;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Api(tags = "6购物车模块")
@RestController
public class ShopController {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private CommodityMapper commodityMapper;


    @RequestMapping("/shop/selectToShopcar")
    public List<Shopcar> getShopcar(int id){
        return shopMapper.getshopcar(id);
    }

    //删除购物车商品
    @RequestMapping("/shop/deleteToShopcar")
    public String deleteShopcarOne(int id){
       shopMapper.deleteShopcarOne(id);
        return "ok";
    }
        //使商品数量加一
    @RequestMapping("/shop/delToShopcarSum")
    public String delToShopcarSum(int id){
        int row = shopMapper.delToShopcarSum(id);
        if (row==1)
        return "ok";
        return  "no";
    }
    //使商品数量减一
    @RequestMapping("/shop/addToShopcarSum")
    public String addToShopcarSum(int id){
        int row = shopMapper.addToShopcarSum(id);
        if (row==1)
            return "ok";
        return  "no";
    }


    @RequestMapping("/shop/insertToShopcar")
    public String insertToShopcar(int comid,Integer uid){
        //购物车里要是有,就加一即可
        Shopcar s = shopMapper.selectByUserid(comid,uid);
        if (s!=null){
            shopMapper.insertToShopcarOne(comid,uid);
            return "加一成功!";
        }

        Commodity com = commodityMapper.SelectById(comid);
        System.out.println("comcomcomc=="+com);
        if (com!=null) {
            Shopcar shopcar = new Shopcar();
            shopcar.setName(com.getName());
            shopcar.setIntro(com.getIntro());
            shopcar.setSum(com.getSum());
            shopcar.setUserid(uid);
            shopcar.setComid(comid);
            shopcar.setPrice(com.getPrice());
            shopcar.setSalesnum(com.getSalesnum());
            shopcar.setCreated(new Date());
            shopcar.setClassId(com.getClassId());
            shopcar.setUrl(com.getUrl());
            shopMapper.insertToShopcar(shopcar);
            return "ok";
        }return "该商品已下架!";
    }
}
